"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectRefundCtrl
 * @description
 * # ProjectRefundCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectRefundCtrl", function (
    $scope,
    $location,
    openApi,
    Util,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    var datalist = [
      {
        key: "内部借款",
        value: 12
      },
      {
        key: "保证金",
        value: 3
      },
      {
        key: "押金",
        value: 4
      },
      {
        key: "缴纳",
        value: 5
      },
      {
        key: "成本",
        value: 6
      },
      {
        key: "收入",
        value: 7
      },
      {
        key: "已付款",
        value: 10
      },
      {
        key: "农民工工资保证金",
        value: 14
      }
    ];
    var datalist2 = [
      {
        key: "垫付款",
        value: 2
      },
      {
        key: "预借款",
        value: 8
      },
      {
        key: "缴纳",
        value: 5
      },
      {
        key: "成本",
        value: 6
      },
      {
        key: "收入",
        value: 7
      },
      {
        key: "保证金",
        value: 3
      },
      {
        key: "押金",
        value: 4
      }
    ];
    var datalist3 = [
      {
        key: "结算款",
        value: 11
      }
    ];
    var datalist4 = [
      {
        key: "农民工工资付款",
        value: 16
      }
    ];

    // 选择项目收款
    $scope.chooseSk = function () {
      var params = $scope.param;
      if (!params.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      var projectParams = {
        projectId: params.projectId,
        formKey: "Xm_ProjectGathering",
        fieldName: "gatheringNo"
      };
      // 清掉关联的编号
      Util.savaParamForReview(params, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    // 选择款项类别
    $scope.costTypechange = function () {
      if ($scope.param.refundTarget === 3) {
        $scope.param.refundType = 11;
      } else {
        $scope.param.refundType = "";
      }
      getCostTypeList();
    };
    var getCostTypeList = function () {
      switch (parseInt($scope.param.refundTarget,10)) {
        case 1:
          $scope.costTypeList = datalist2;
          break;
        case 2:
          $scope.costTypeList = datalist;
          break;
        case 3:
          $scope.costTypeList = datalist3;
          break;
        case 4:
          $scope.costTypeList = datalist4;
          break;
        default:
          break;
      }
    };
    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };
    $scope.deleteItem = function (index) {
      $scope.param.details.splice(index, 1);
      $scope.index = -1;
    };
    $scope.swipe = function (index) {
      $scope.index = index;
    };
    $scope.resetlist = function () {
      $scope.index = -1;
    };

    $scope.submit = function (operationId) {
      $scope.param.relatedIds = $scope.param.details.map(function (item) {
        return item.refundId;
      });
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.addprojectRefund;
      options.param = angular.copy($scope.param, {});
      delete options.param.details;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }
      if (!$scope.param.refundTarget) {
        Util.Toast("请选择退回到");
        return;
      }
      if (!$scope.param.refundType) {
        Util.Toast("请选择款项类别");
        return;
      }
      if (!$scope.param.amount) {
        Util.Toast("请填写退回金额");
        return;
      }
      if (!$scope.param.refundReason) {
        Util.Toast("请填写退回事由");
        return;
      }

      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    function getList() {
      var param = {};
      param.type = "项目退款类型";
      Util.getKeyValue(param, function (data) {
        $scope.costSourceType = data.data;
      });
    }
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        $scope.param = Object.assign({}, $scope.param, data, projectreldata);
        $scope.param.details = $scope.param.details || [];
        if ($scope.param.refundTarget) {
          $scope.param.refundTarget = "" + $scope.param.refundTarget
        }
        getCostTypeList();
      });
      getList()
    };
    // 初始化
    init();
  });
